#define _CRT_SECURE_NO_WARNINGS

#include"String.h"

namespace cx
{

	const size_t string::npos = -1;

	string::iterator string::begin()
	{
		return _str;
	}
	string::iterator string::end()
	{
		return _str + _size;
	}

	string::const_iterator string::begin() const
	{
		return _str;
	}
	string::const_iterator string::end() const
	{
		return _str + _size;
	}


	string::string(const char* s)
		:_size(strlen(s))
	{
		_capacity = _size;
		_str = new char[_capacity + 1];
		strcpy(_str, s);
	}

	string::string(const string& s)
	{
		_size = s._size;
		_capacity = s._capacity;

		_str = new char[_capacity + 1];
		strcpy(_str, s._str);
	}

	string& string::operator=(const string& s)
	{
		if (this != &s)
		{
			char* tmp = new char[s._capacity + 1];
			strcpy(tmp, s._str);
			_size = s._size;
			_capacity = s._capacity;
			delete[] _str;
			_str = tmp;
		}
		return *this;
	}

	string::~string()
	{
		delete[] _str;
		_str = nullptr;
		_capacity = _size = 0;
	}
	
	const char* string::c_str() const
	{
		return _str;
	}

	size_t string::size() const
	{
		return _size;
	}

	char& string::operator[] (size_t pos)
	{
		assert(pos < _size);
		return _str[pos];
	}

	const char& string::operator[] (size_t pos) const
	{
		assert(pos < _size);
		return _str[pos];
	}

	void string::reserve(size_t n)
	{
		if (_capacity < n)
		{
			_capacity = n;
			char* tmp = new char[n+1];
			strcpy(tmp,_str);
			delete[] _str;
			_str = tmp;
		}

	}

	void string::push_back(char ch)
	{
		if (_size == _capacity)
		{
			int newCapacity = _capacity == 0 ? 4 : _capacity * 2;
			reserve(newCapacity);
		}
		_str[_size] = ch;
		_str[_size + 1] = '\0';
		_size++;
	}

	void string::append(const char* str)
	{
		int len = strlen(str);
		if (len+_size > _capacity)
		{
			reserve(len + _size);
		}

		strcpy(_str + _size, str);
		_size += len;
	}

	string& string::operator+=(char ch)
	{
		push_back(ch);
		return *this;
	}

	string& string::operator+=(const char* str)
	{
		append(str);
		return *this;
	}

	void string::insert(size_t pos, char ch)
	{
		assert(pos < _size);


		if (_size == _capacity)
		{
			int newCapacity = _capacity == 0 ? 4 : _capacity * 2;
			reserve(newCapacity);
		}

		size_t end = _size + 1;
		while (end > pos)
		{
			_str[end] = _str[end - 1];
			end--;
		}
		_str[pos] = ch;
		++_size;

		/*for (int i = _size - 1; i >= pos; --i)
		{
			_str[i + 1] = _str[i];
		}
		_str[pos] = ch;
		_size++;
		_str[_size] = '\0';*/
	}

	void string::insert(size_t pos, const char* str)
	{
		assert(pos < _size);
		int len = strlen(str);
		if (len + _size > _capacity)
		{
			reserve(len + _size);
		}

		size_t end = len + _size;
		while (end > pos)
		{
			_str[end] = _str[end - len];
			end--;
		}
		memcpy(_str + pos, str, len);
		_size += len;
	}
	void string::erase(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len + pos >= _size)
		{
			_str[pos] = '\0';
			_size -= len;
		}
		else
		{
			for (int i = pos + len; i <= _size; i++)
			{
				_str[i - len] = _str[i];
				_size--;
			}
		}
	}

	size_t string::find(char ch, size_t pos)
	{
		assert(pos < _size);
		for (int i = pos; i < _size; ++i)
		{
			if (_str[i] == ch)
				return i;
		}
		return npos;


	}
	size_t string::find(const char* str, size_t pos)
	{
		assert(pos < _size);
		char* tmp = strstr(_str + pos, str);
		if (nullptr != tmp)
		{
			return tmp - _str;
		}
		return npos;
	}

	void string::swap(string& s)
	{
		std::swap(_str, s._str);
		std::swap(_capacity, s._capacity);
		std::swap(_size, s._size);
	}

	bool string::operator>(const string& s)const
	{
		return strcmp(_str, s._str) > 0;
	}
	bool string::operator>=(const string& s)const
	{
		return _str > s._str || _str == s._str;
	}
	bool string::operator<(const string& s)const
	{
		return !(_str >= s._str);
	}
	bool string::operator<=(const string& s)const
	{
		return _str < s._str || _str == s._str;
	}
	bool string::operator==(const string& s)const
	{
		return strcmp(_str, s._str) == 0;
	}
	bool string::operator!=(const string& s)const
	{
		return !(_str == s._str);
	}



	string string::substr(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len + pos > _size)
		{
			string tmp(_str + pos);
			return tmp;
		}
		else
		{
			string tmp;
			tmp.reserve(len);
			for (int i = 0; i < len; i++)
			{
				tmp += _str[i + pos];
			}

			return tmp;
		}
	}

	void string::clear()
	{
		_size = 0;
		_str[0] = '\0';
	}


	ostream& cx::operator<<(ostream& out,const string& s)
	{
		for (int i = 0; i < s.size(); i++)
		{
			out << s[i];
		}
		out << endl;
		return out;
	}
	istream& cx::operator>>(istream& in, string& s)
	{
		s.clear();
		char ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			s += ch;
			ch = in.get();
		}
		return in;
	}
}